﻿/*
department : beacon
write by   : wuweiwei
github     : www.github.com/flybirdsoft
date       : 2015/10/28

tip: this lib is for mobile
*/


(function(win){
	"use strict";
	var wPress = function(options){
		this.version = "1.0";
		this.isTouch = false; /*在touchend事件里判断此值*/

		this.tId = -1;

		this.target = null; /*在 bindEvent事件里动态设置,表示用户触屏的DOM对象*/
		this.targetIndex = -1; /*target 在节点流中的索引顺序 */
		this.children = null;

		this.position = {};
		if(options!=undefined)
		{
			this.register(options);
		}
	};

	wPress.prototype.register = function(options){
		/*
		options={
			container:DOM,
			interval : n, 
			data : [],
			isiscroll:false,
			ontouch : function(object){
			
			},
			onup : function(object){
		
			}
		}
		*/

		this.options = options;
		this.container = this.options.container; /*DOM*/
		this.options.interval = options.interval || 700;
		this.options.ontouch = options.ontouch || function(){};
		this.options.onup = options.onup || function(){};
		this.children = this.getChildren();
		if(this.options.data!=undefined)
		{
			this.setDataIndex();
		}
		this.bindEvent();
	};

	wPress.prototype.setDataIndex = function(){
		var i;
		var len = this.options.data.length;
		var data = this.options.data;

		for(i=0;i<len;i++)
		{
			this.children[i].setAttribute("wpress-dataIndex",i);
		}
	};

	wPress.prototype.getChildren = function(){
		var i;
		var childNodes = this.options.container.childNodes;
		var len = childNodes.length;
		var children = [];
		for(i=0;i<len;i++)
		{
			if(childNodes[i].nodeType == 1)
			{
				children.push(childNodes[i]);
			}
		}
		return children;
	}

	wPress.prototype.bindEvent = function(){
		var th = this;
		this.container.addEventListener("touchstart",function(e){
			var object;
			var evt = e || window.event;
			var target = evt.target || evt.srcElement;
			th.target = target;
			th.touchTarget  = th.findSelectedItem(th.target);
			th.targetIndex = th.findSelectedIndex(th.target);
			if(th.options.data!=undefined && th.options.data.length!=undefined)
			{
				object = th.options.data[th.targetIndex];
			}
			th.row = object;

			
			th.position.x = e.touches[0].clientX;
			th.position.y = e.touches[0].clientY;

			th.tId = setTimeout(function(e){
				th.isTouch = true;
				th.options.ontouch.call(th,evt); /*callback*/
			},th.options.interval);
		},false);

		this.container.addEventListener("touchend",function(e){
			clearTimeout(th.tId);
			var object;
			var evt = e || window.event;
			var target = evt.target || evt.srcElement;
			th.target = target;
			th.touchTarget  = th.findSelectedItem(th.target);
			th.targetIndex = th.findSelectedIndex(th.target);
			if(th.options.data!=undefined && th.options.data.length!=undefined)
			{
				object = th.options.data[th.targetIndex];
			}
			th.row = object;

			if(th.isTouch)
			{
				th.isTouch = false;
				th.options.onup.call(th,evt);
			}
			

		},false);

		this.container.addEventListener("touchmove",function(e){
			var pos ={};
			th.isTouch = false;
			pos.x = e.touches[0].clientX;
			pos.y = e.touches[0].clientY;
			if(Math.abs(th.position.x-pos.x)>=7 || Math.abs(th.position.y-pos.y)>=7)
			{
				clearTimeout(th.tId);
			}
		},false);

	};

	wPress.prototype.findSelectedIndex = function(dom){
		/*返回DOM节点在DOM列表里的索引位置*/
		var i;
		var tmpDom = dom;
		try
		{
			while(!tmpDom.hasAttribute("wpress-dataIndex"))
			{
				tmpDom = tmpDom.parentNode;
			}
			return tmpDom.getAttribute("wpress-dataIndex");
		}
		catch(e)
		{
			return -1;
		}
	};

	wPress.prototype.findSelectedItem = function(dom){
		/*返回DOM节点在DOM列表里的索引位置*/
		var i;
		this.children = this.getChildren();
		var len = this.children.length;

		for(i=0;i<len;i++)
		{
			if(this.children[i].contains(dom) || this.children[i]==dom)
			{
				return this.children[i];
				break;
			}
		}

	};

	win["wPress"] = wPress;

})(window);

/*

demo:

var list =[
	{
		id:"001",
		age:33
	},
	{
		id:"002",
		age:35
	}
];

调用方法一:
var press = new wPress();
press.register({
	container : $(".pad")[0],
	interval  : 700,
	data      : list,
	ontouch   : function(e){
		alert(this.touchTarget);
		alert(JSON.stringify(this.row));
	},
	onup : function(e){}
});

调用方法二:
var press = new wPress({
	container : $(".pad")[0],
	interval  : 700,
	data      : list,
	ontouch   : function(e){
		alert(this.touchTarget);
		alert(JSON.stringify(this.row));
	},
	onup : function(e){}
});


注意：可选参数有:interval,data;
ontouch : function(e){
	this.touchTarget  参数“container”里的当前触发的child DOM
	this.target       当前用户触发的DOM
	this.row          对应参数“data”里当前DOM的值,值为json
	
}

*/